/**
 * Created by Administrator on 2017/10/11
 */
import {message, Col, Button} from 'antd';
import * as mobx from 'mobx';
import {WeaSelect,WeaDialog,WeaTop,WeaScope,WeaRightMenu,WeaFormItem,WeaLocaleProvider} from 'ecCom';
import {inject,observer} from 'mobx-react';
import allStore from '../../stores';
import Browser from '../../public/wf-browser/index';
import cloneDeep from 'lodash/cloneDeep';
const getLabel = WeaLocaleProvider.getLabel;


@observer
export default class Main extends React.Component {
    constructor(props) {
        super(props);

        // this.state = {
        //     visible: props.visible,
        //     objTypeValue: '57',  //对象类型，默认57为部门
        //     jobsLevel: '0',  //岗位级别
        //     objValue: '',  //初始化对象为空
        //     objValueSpan: '',  //初始化对象名字为空
        //     objValue002: '', //为岗位时，岗位级别初始化为空
        //     adviceValue: '1', //流转意见，1为可查看，2为不可查看
        //     scopeMin: [0, 255], //安全级别
        //     scopeMax: [0, 255], //安全级别
        //     scopeStartValue: 0,  //安全级别初始化
        //     scopeEndValue: 100,  //安全级别初始化
        //     departmentid: [], //为部门时id
        //     jobsLevelid: [],   //为岗位时id
        //     roleid: [], // 为角色时id
        //     rolelevel: '0', //为角色时，对象级别。0为部门，1为分部，2为总部
        //
        //     userid: [], //为人力资源时id
        //     subids: [], //为分部时id
        //     jobid: [], //为岗位时id
        //     joblevel: 0, //为岗位时，岗位级别。0为总部，194为指定分部，57为指定部门
        //
        //     relatedshareid_6: [], //为岗位时，指定分部或者部门的id
        //     allObjInfo: [],   //选择对象的跳转链接，名称等数据,all是对象的数据，job是为岗位时，岗位级别的数据
        //     jobObjInfo: []
        // };
    }

    handleOk() {

        const {wfAddShareStore} = allStore;
        let {relatedshareid_6,objValue,objTypeValue,adviceValue,seclevel,seclevelMax,rolelevel,allObjInfo,jobObjInfo,jobsLevel} = wfAddShareStore;

        allObjInfo = allObjInfo.slice();
        jobObjInfo = jobObjInfo.slice();
        const iscanread = adviceValue;

        if(!seclevel){
            seclevel = 0;
        }
        if(!seclevelMax){
            if(Number(seclevelMax) === 0){
                seclevelMax = 0;
            }else {
                seclevelMax = 255;
            }
        }

        let joblevel;
        if (jobsLevel === '57') {
            joblevel = 0;
        } else if (jobsLevel === '194') {
            joblevel = 1;
        } else if (jobsLevel === '0') {
            joblevel = 2;
        }

        if(objTypeValue !== '0' && objValue.length === 0){
            message.warning(getLabel(384146,'红色*号为必填项'));
            jQuery('.showAlert001 .ant-select-search__field')[0].focus();
            return 0;
        }

        if(objTypeValue === '278' && joblevel !== 2){
            if (relatedshareid_6.length === 0) {
                message.warning(getLabel(384146,'红色*号为必填项'));
                jQuery('.showAlert002 .ant-select-search__field')[0].focus();
                return 0;
            }
        }

        if(seclevelMax < seclevel){
            message.warning(getLabel(385085,'安全级别设置有误，请重新设置'));
            return 0;
        }



        let permissiontype;

        switch (Number(objTypeValue)){
            case 194 :
                permissiontype = 6;
                break ;
            case 0 :
                permissiontype = 3;
                break ;
            case 17 :
                permissiontype = 5;
                break ;
            case 65 :
                permissiontype = 2;
                break ;
            case 278 :
                permissiontype = 7;
                break ;
            default :
                permissiontype = 1;
                break ;
        }

        this.initDatas();

        this.props.handleOk({
            permissiontype,
            iscanread,
            objValue,
            seclevel,
            seclevelMax,
            rolelevel,
            joblevel,
            relatedshareid_6
        }, allObjInfo, jobObjInfo);

    }

    handleCancel() {
        this.initDatas();
        this.props.handleCancel();

    }

    initDatas() {
        const {wfAddShareStore} = allStore;
        wfAddShareStore.init();
    }

    getFooter() {
        let btns = [];
        btns.push(
            <Button key="save" type="primary" onClick={() => this.handleOk()}>{getLabel(86,'保存')}</Button>,
        )
        return btns;
    }

    getAdviceOptions() {   //流转意见下拉框options
        let adviceOptions = [
            {
                key: "1",
                selected: true,
                showname: getLabel(20306,"可查看")
            }, {
                key: "0",
                selected: false,
                showname: getLabel(82613,"不可查看")
            }
        ];
        return adviceOptions;
    }

    getJobsLevelOptions() {
        let jobsLevelOptions = [  //岗位类型下拉框options
            {
                key: "0",
                selected: true,
                showname: getLabel(140,"总部")
            }, {
                key: "194",
                selected: false,
                showname: getLabel(19437,"指定分部")
            }, {
                key: "57",
                selected: false,
                showname: getLabel(19438,"指定部门")
            }
        ];
        return jobsLevelOptions;
    }
    getLevelOptions() {
        let levelOptions = [  //角色对象级别options
            {
                key: "0",
                selected: true,
                showname: getLabel(124,"部门")
            }, {
                key: "1",
                selected: false,
                showname: getLabel(141,"分部")
            }, {
                key: "2",
                selected: false,
                showname: getLabel(140,"总部")
            }
        ];
        return levelOptions;
    }
    getOptions() {
        let options = [    //对象类型下拉框options
            {
                key: "57",
                selected: true,
                showname: getLabel(124,"部门")
            }, {
                key: "194",
                selected: false,
                showname: getLabel(141,"分部")
            }, {
                key: "0",
                selected: false,
                showname: getLabel(1340,"所有人")
            }, {
                key: "17",
                selected: false,
                showname: getLabel(383424,"人力资源")
            }, {
                key: "65",
                selected: false,
                showname: getLabel(122,"角色")
            }, {
                key: "278",
                selected: false,
                showname: getLabel(6086,"岗位")
            }
        ];
        return options;
    }

    getMoreBtns () {
        let btns = {
            datas: [],
            collectParams: {
                favname: getLabel(383655,'添加共享'),
                favouritetype: 2,
                objid: 0,
                importantlevel: 1,
            }
        };

        btns.datas.push(
            {
                key: '1',
                disabled: false,
                icon: <i className='icon-coms-Preservation'/>,
                content: getLabel(86,'保存'),
                onClick: key => this.handleOk(),
            }
        )

        return btns;
    }

    render() {
        const {wfAddShareStore} = allStore;
        const {visible} = this.props;
        const {objTypeValue,rolelevel,jobsLevel,objValue,relatedshareid_6,seclevel,seclevelMax,adviceValue,replaceDatas} = wfAddShareStore;

        const layout = {
            labelCol : {span: 5, offset: 4},
            wrapperCol: {span: 12}
        };
        return (
            <div>
                <WeaDialog
                    className="wf-addShareModal"
                    title={getLabel(383655,'添加共享')}
                    icon="icon-coms-workflow"
                    iconBgcolor='#0079DE'
                    visible={visible}
                    maskClosable={false}
                    buttons={this.getFooter()}
                    onCancel={() => this.handleCancel()}
                    style={ {width: "600px", height: "250px"} }
                    moreBtn={this.getMoreBtns()}
                    hasScroll={true}
                >
                    <WeaRightMenu
                        {...this.getMoreBtns()}
                    >

                    <WeaFormItem
                        label={getLabel(21956,"对象类型")}
                        {...layout}
                    >
                        <WeaSelect
                            options={this.getOptions()}
                            value={objTypeValue}
                            onChange={(v) => {
                                wfAddShareStore.init(v);
                            }}
                        />
                    </WeaFormItem>

                    {
                        objTypeValue !== '0' &&
                        <WeaFormItem
                            label={getLabel(106,"对象")}
                            {...layout}
                        >
                            <Col span={objTypeValue === '65' ? 16 : 24 }>
                                <div className="showAlert001">
                                    <Browser
                                        type={objTypeValue}
                                        viewAttr={3}
                                        value={objValue}
                                        // replaceDatas={replaceDatas.obj.slice()}
                                        onChange={(ids, names, datas, linkUrl) => {
                                            const idsArr = ids.split(',');
                                            const namesArr = names.split(',');
                                            let objInfos = [];
                                            idsArr.forEach((item, index) => {
                                                objInfos.push({
                                                    link: linkUrl.linkUrl + item,
                                                    name: namesArr[index],
                                                    id: item
                                                });
                                            })

                                            // const newReplaceDatas = cloneDeep(mobx.toJS(replaceDatas));
                                            // newReplaceDatas.obj = [...datas];
                                            // wfAddShareStore.setReplaceDatas(newReplaceDatas);
                                            wfAddShareStore.objValueChange(ids,objInfos);

                                        }}
                                    />
                                </div>
                            </Col>
                            {
                                objTypeValue === '65' &&
                                <Col span={7} offset={1}>
                                    <WeaSelect
                                        options={this.getLevelOptions()}
                                        value={rolelevel}
                                        onChange={(v) => {
                                            wfAddShareStore.rolelevelChange(v);
                                        }}
                                    />
                                </Col>
                            }
                        </WeaFormItem>
                    }
                    {
                        (objTypeValue !== '17' && objTypeValue !== '278') &&
                        <WeaFormItem
                            label={getLabel(683,"安全级别")}
                            {...layout}
                        >
                            <WeaScope
                                isMobx
                                min={[0,255]}
                                max={[0,255]}
                                value={[seclevel,seclevelMax]}
                                onChange={(v) => {
                                    wfAddShareStore.seclevelChange(v[0],v[1]);
                                }}
                            />
                        </WeaFormItem>
                    }
                    {
                        objTypeValue === '278' &&
                        <WeaFormItem
                            label={getLabel(6086,"岗位")}
                            {...layout}
                        >
                            <Col span={jobsLevel !== '0' ? 7 : 24}>
                                <WeaSelect
                                    options={this.getJobsLevelOptions()}
                                    value={jobsLevel}
                                    onChange={(v) => {
                                        wfAddShareStore.jobslevelChange(v);
                                    }}
                                />
                            </Col>
                            {
                                jobsLevel !== '0' &&
                                <Col span={16} offset={1}>
                                    <div className="showAlert002">
                                        <Browser
                                            type={jobsLevel}
                                            viewAttr={3}
                                            value={relatedshareid_6}
                                            // replaceDatas={replaceDatas.jobsLevel.slice()}
                                            onChange={(ids, names, datas, linkUrl) => {
                                                const idsArr = ids.split(',');
                                                const namesArr = names.split(',');
                                                let job = [];
                                                idsArr.forEach((item, index) => {
                                                    job.push({
                                                        link: linkUrl.linkUrl + item,
                                                        name: namesArr[index],
                                                    });
                                                })

                                                // const newReplaceDatas = cloneDeep(mobx.toJS(replaceDatas));
                                                // newReplaceDatas.jobsLevel = [...datas];
                                                // wfAddShareStore.setReplaceDatas(newReplaceDatas);
                                                wfAddShareStore.relatedshareid_6Change(ids,job);

                                            }}
                                        />
                                    </div>
                                </Col>
                            }
                        </WeaFormItem>
                    }

                    <WeaFormItem
                        label={getLabel(125734,"流转意见")}
                        {...layout}
                    >
                        <WeaSelect
                            options={this.getAdviceOptions()}
                            value={adviceValue}
                            onChange={(v) => {
                                wfAddShareStore.adviceValueChange(v);
                            }}
                        />
                    </WeaFormItem>
                    </WeaRightMenu>
                </WeaDialog>
            </div>
        );
    }
};